import {summonerInfoStore} from "@/renderer/main-window/stores/storeSummonerInfo.js";
import {
    champion_store, item_store, map_store, perk_style_store, perks_store, queue_store, spell_store, staticAssertsStore
} from "@/renderer/main-window/stores/storeAsserts.js";
import {storeSystemCertificateInfo} from "@/renderer/main-window/stores/storeSystemCertificateInfo.js";
import {
    championSelect,
    gameFlowSessionStore,
    gaming,
    lobby
} from "@/renderer/share/stores/sotreCurrentGameSession.js";
import {ElMessage} from "element-plus";


//初始化当前用户的信息&配置
export async function initCurrentSummonerInfo() {
    await window.api.summoner.currentSummoner().then(res => {
        summonerInfoStore().setCurrentSummonerInfo(res.data)
    })

    await window.api.summoner.currentSummonerProfile().then(res => {
        summonerInfoStore().setCurrentSummonerInfoProfile(res.data)
    })

}

export async function initGameFLowInfo() {
    await window.api.gameflow.currentPhase().then(res => {
        gameFlowSessionStore().setGamePhase(res.data);
    })

    if (gameFlowSessionStore().gamePhase === championSelect) {
        window.api.gameflow.championSelectSession().then(res => {
            gameFlowSessionStore().setChampSelectSession(res.data);
        }).catch(err => {
            ElMessage.error({
                message: `获取当前英雄选择会话信息失败${err}`
            });
        })
    }

    if (gameFlowSessionStore().gamePhase === lobby) {
        window.api.gameflow.getLobbySession().then(res => {
            gameFlowSessionStore().setLobby(res.data);
        }).catch(err => {
            ElMessage.error({
                message: `获取当前房间会话信息失败${err}`
            });
        })
    }

    if (gameFlowSessionStore().gamePhase === gaming) {
        window.api.gameflow.getGameFlowSession().then(res => {
            gameFlowSessionStore().setGameFlow(res.data);
        }).catch(err => {
            ElMessage.error({
                message: `获取当前游戏session失败${err}`
            });
        })
    }
    //如果在英雄选择阶段 那么直接初始化当前房间的信息
}

//初始化用户配置
export async function initUserConfig() {

}


export function initSystemInfo() {
    window.electron.ipcRenderer.invoke('proxy-server/baseUrl')
        .then(val => {
            storeSystemCertificateInfo().setImgProxyPrefix(val);
        })

    //同步客户端是否连接的状态
    window.electron.ipcRenderer.invoke('lcu-server/status/pull')
        .then(res => {
            const {connectStatus} = res;
            console.log(`主动拉取到客户端状态:${connectStatus}`);
            storeSystemCertificateInfo().setLcuServerStatus(connectStatus);
        })
}

/**
 * 初始化游戏数据
 */
export async function initGameBaseData() {
    //队列信息
    await window.api.baseData.queue_json().then(res => {
        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: queue_store, id: item.id, data: item
            })
        })
    })

    //装备信息
    await window.api.baseData.item_json().then(res => {
        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: item_store, id: item.id, data: item
            })
        })
    })

    //天赋类型
    await window.api.baseData.perkstyle_json().then(res => {

        res?.data?.styles?.forEach((item) => {
            staticAssertsStore().addInfo({
                key: perk_style_store, id: item.id, data: item
            })
        })

    })


    //天赋详细
    await window.api.baseData.perks_json().then(res => {
        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: perks_store, id: item.id, data: item
            })
        })

    })


    //英雄
    await window.api.baseData.champion_summoner().then(res => {
        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: champion_store, id: item.id, data: item
            })
        })

    })

    //召唤师技能
    await window.api.baseData.spell_json().then(res => {

        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: spell_store, id: item.id, data: item
            })
        })

    })
    //地图数据
    window.api.baseData.map_json().then(res => {

        res.data.forEach((item) => {
            staticAssertsStore().addInfo({
                key: map_store, id: item.id, data: item
            })
        })

    })

    window.api.baseData.map_asserts().then(res => {
    })

}